package com.rt.easyjava.common.dozer.converter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rt.easyjava.common.utils.others.InvokeMethodUtils;
import com.rt.easyjava.common.utils.others.StringUtils;
import org.dozer.DozerConverter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 枚举类型在dozer中的转换工具类
 * Dto中用Object接收json对象，结构如：deleteflag：{'key':1, 'name':'有效'}, 前端展示处理如：row.deleteflag.name
 * Created by wxy on 2020/4/8.
 */
public class EnumMultiConverter extends DozerConverter<String,Object> {


    public EnumMultiConverter() {
        super(String.class, Object.class);
    }

    // Bean -> Dto
    @Override
    public Object convertTo(String source, Object destination) {
        if(source == null) {
            JSONObject json = new JSONObject();
            json.put("id", new ArrayList<>());
            json.put("name", "");
            return json;
        }
        try{
            String enumClass = getParameter();
            String s1 = source.replaceAll(" ", "").replaceAll("\\[", "").replaceAll("\\]", "");
            if (StringUtils.isNull(s1)) {
                JSONObject json = new JSONObject();
                json.put("id", new ArrayList<>());
                json.put("name", "");
                return json;
            }
            String[] split = s1.split(",");
            StringBuilder sb = new StringBuilder();
            List<Integer> idList = new ArrayList<>();
            for(int i=0;i<split.length;i++){
                String s = split[i];
                int i1 = Integer.parseInt(s);
                Object object = InvokeMethodUtils.invokeStaticMethod(enumClass,"explain", i1);
                String name = String.valueOf(object);
                sb.append(name).append(",");
                idList.add(i1);
            }
            //删除最后一个字符“,”
            if (sb.length() > 0) {
                sb.deleteCharAt(sb.length() - 1);
            }

            JSONObject json = new JSONObject();
            json.put("id", idList);
            json.put("name", sb.toString());
            return json;
        }catch (Exception e) {
            JSONObject json = new JSONObject();
            json.put("id", new ArrayList<>());
            json.put("name", "");
            return json;
        }
    }

    // Dto -> Bean
    @Override
    public String convertFrom(Object source, String destination) {
//        if(StringUtils.isNull(source)) return null; // mapper转换dto->bean作查询条件时，非查询条件需要为null
//        // 行内编辑保存时，传过来的是一个map: {id=1, name=xxx}
//        if(source instanceof Map) {
//            return Integer.parseInt(((Map)source).get("id").toString());
//        }
//        return Integer.parseInt(source.toString());

        if(StringUtils.isNull(source)) return null; // mapper转换dto->bean作查询条件时，非查询条件需要为null
        // 行内编辑保存时，传过来的是一个map: {id=1, name=xxx}
        if(source instanceof Map) {
            return ((Map)source).get("id").toString();
        }
        return source.toString();
    }

}
